//
//  XJViewController.m
//  ttpod
//
//  Created by dllo on 16/3/21.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "XJViewController.h"
#import "Header.h"

#import "GLCollectionViewCellForFind.h"
#import "GLCollectionViewCellForMine.h"
#import "GLCollectionViewCellForRecommend.h"
#import "GLCollectionViewCellForWhite.h"
#import "Header.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height


@interface XJViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UISegmentedControl *headerForSegment;

@property (nonatomic, retain) UICollectionView *collectionViewForMain;  /**< 三页的collection */

@property (nonatomic, retain) UICollectionViewFlowLayout *flowLayoutForMain;  /**< 三页的collection的flowLayout */

@property (nonatomic, retain) UIView *viewForSong;  /**< 页面下方的小白条 */

@property (nonatomic, retain) UICollectionView *collectionViewForWhite;  /**< 白条上的collection */

@property (nonatomic, retain) UICollectionViewFlowLayout *flowLayoutForWhite;  /**< 白条上的flowlayout */

@property (nonatomic, retain) UIButton *buttonForBegin;  /**< 开始的按钮 */

@property (nonatomic, retain) UIButton *buttonForMenu;  /**< 点击弹出菜单 */




@end

@implementation XJViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.navigationController.navigationBar setBarTintColor:HEAD_COLOR];
    
    // 设置seg被选中时的字体大小和颜色.
    NSDictionary *selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]};
    [self.headerForSegment setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];
    
    [self.headerForSegment addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    
    // 设置seg被未选中时的字体大小和颜色.
    NSDictionary *unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],NSForegroundColorAttributeName:[UIColor colorWithRed:255 / 255.f green:166 / 255.f blue:166 / 255.f alpha:1]};
    [self.headerForSegment setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
    
    self.headerForSegment.tintColor = [UIColor clearColor];
    
    [self createLayout];
    
    [self createCollection];
    
    [self createView];
    
    [self createFlowLayoutForWhite];
    
    [self createViewForCollectionView];
    
    [self createButtonForBegin];

    [self createButtonForMenu];
    
    
    
}

#pragma mark - 创建layout
- (void)createLayout {
    
    self.flowLayoutForMain = [[UICollectionViewFlowLayout alloc] init];
    
    self.flowLayoutForMain.itemSize = CGSizeMake(WIDTH, HEIGHT - 64 - 49);
    
    self.flowLayoutForMain.minimumLineSpacing = 0;
    
    self.flowLayoutForMain.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.flowLayoutForMain.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
}

#pragma mark - 创建collection
- (void)createCollection {
    
    self.collectionViewForMain = [[UICollectionView alloc] initWithFrame:CGRectMake(0,64, WIDTH, HEIGHT - 49 - 64) collectionViewLayout:self.flowLayoutForMain];
    
    [self.view addSubview:self.collectionViewForMain];
    
    self.collectionViewForMain.bounces = NO;
    
    /** 滑动条 */
    self.collectionViewForMain.showsHorizontalScrollIndicator = NO;
    
    self.collectionViewForMain.delegate = self;
    
    self.collectionViewForMain.dataSource = self;
    
    self.collectionViewForMain.pagingEnabled = YES;
    
    [self.collectionViewForMain registerClass:[GLCollectionViewCellForMine class] forCellWithReuseIdentifier:@"poolForMine"];
    
    [self.collectionViewForMain registerClass:[GLCollectionViewCellForRecommend class] forCellWithReuseIdentifier:@"poolForRecommend"];
    
    [self.collectionViewForMain registerClass:[GLCollectionViewCellForFind class] forCellWithReuseIdentifier:@"poolForFind"];
    
}

#pragma mark - 控制歌曲的小白条
- (void)createView {
    
    self.viewForSong = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT - 49, WIDTH, 49)];
    
    [self.view addSubview:self.viewForSong];
    
    self.viewForSong.backgroundColor = [UIColor whiteColor];
    
    [self.viewForSong bringSubviewToFront:self.view];
    
}

#pragma mark - 白条上的collectionView的layout
- (void)createFlowLayoutForWhite {
    
    self.flowLayoutForWhite = [[UICollectionViewFlowLayout alloc] init];
    
    self.flowLayoutForWhite.itemSize = CGSizeMake((WIDTH / 3) * 2, 49);
    
    self.flowLayoutForWhite.minimumLineSpacing = 0;
    
    self.flowLayoutForWhite.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.flowLayoutForWhite.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
}

#pragma mark - 白条上的collectionView
- (void)createViewForCollectionView {
    
    self.collectionViewForWhite = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, (WIDTH / 3) * 2, 49) collectionViewLayout:self.flowLayoutForWhite];
    
    [self.viewForSong addSubview:self.collectionViewForWhite];
    
    
    self.collectionViewForWhite.bounces = NO;
    
    /** 滑动条 */
    self.collectionViewForWhite.showsHorizontalScrollIndicator = NO;
    
    self.collectionViewForWhite.delegate = self;
    
    self.collectionViewForWhite.dataSource = self;
    
    self.collectionViewForWhite.pagingEnabled = YES;
    
    [self.collectionViewForWhite registerClass:[GLCollectionViewCellForWhite class] forCellWithReuseIdentifier:@"poolForWhite"];
    
}

#pragma mark - 创建开始键
- (void)createButtonForBegin {
    
    self.buttonForBegin = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [self.buttonForBegin setImage:[UIImage imageNamed:@"begin"] forState:UIControlStateNormal];
    
    [self.viewForSong addSubview:self.buttonForBegin];
    
    [self.buttonForBegin addTarget:self action:@selector(ActionForBegin) forControlEvents:UIControlEventTouchUpInside];
    
    self.buttonForBegin.frame = CGRectMake(WIDTH - (WIDTH / 3) + 15, 5, 39, 39);
    
}

- (void)ActionForBegin {
    
    
    
}

#pragma mark - 创建歌单
- (void)createButtonForMenu {
    
    self.buttonForMenu = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [self.buttonForMenu setImage:[UIImage imageNamed:@"song"] forState:UIControlStateNormal];
    
    [self.viewForSong addSubview:self.buttonForMenu];
    
    [self.buttonForMenu addTarget:self action:@selector(ActionForMenu) forControlEvents:UIControlEventTouchUpInside];
    
    self.buttonForMenu.frame = CGRectMake(WIDTH - (WIDTH / 3) + 67, 5, 39, 39);
}

- (void)ActionForMenu {
    
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (collectionView == self.collectionViewForMain) {
        
        return 3;
        
    } else {
        
        return 5;
        
    }
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (collectionView == self.collectionViewForMain) {
        
        if (indexPath.item == 0) {
            
            GLCollectionViewCellForMine *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForMine" forIndexPath:indexPath];
            
            cell.backgroundColor = [UIColor redColor];
            
            return  cell;
            
        } else if (indexPath.item == 1) {
            
            GLCollectionViewCellForFind *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForFind" forIndexPath:indexPath];
            
            cell.backgroundColor = [UIColor yellowColor];
            
            return cell;
            
        } else {
            
            GLCollectionViewCellForRecommend *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForRecommend" forIndexPath:indexPath];
            
            cell.backgroundColor = [UIColor blueColor];
            
            return cell;
            
        }
        
    } else {
        
        GLCollectionViewCellForWhite *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForWhite" forIndexPath:indexPath];
        
        cell.backgroundColor = [UIColor orangeColor];
        
        return cell;
        
    }
    
}

- (void)createKVOAction {
    
    // VC观察collection的contentOffset属性.
    [self.collectionViewForMain addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:@"context"];
    
}

#pragma mark - seg点击方法
- (void)segAction:(UISegmentedControl *)seg {
    
    switch (seg.selectedSegmentIndex) {
            
        case 0:
            
            self.collectionViewForMain.contentOffset = CGPointMake(0, 0);
            
            break;
            
        case 1:
            
            self.collectionViewForMain.contentOffset = CGPointMake(WIDTH, 0);
            
            break;
            
        case 2:
            
            self.collectionViewForMain.contentOffset = CGPointMake(WIDTH * 2, 0);
            
            break;
    }
    
}








- (void)viewDidDisappear:(BOOL)animated {
    
   
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
